package org.iteam.xlz.core.service.base;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.base.Area;

public class AreaService extends BaseService<Area>{
	static AreaService instance;
	public AreaService(){}
	List<AreaTree> list=new ArrayList<AreaTree>();
	List<AreaTree> secondList=new ArrayList<AreaTree>();
	List<AreaTree> thirdList=new ArrayList<AreaTree>();
	
	public static AreaService getInstance(){
		if(null==instance){
			instance=new AreaService();
		}
		return instance;
	}
	
	public int getStageByParent(String pid){
		try {
			if(null==pid)
				return 1;
			Area area=super.show("select * from area where 1=1 and aid=?",pid);
			if(null==area.getPid())
				return 2;
			Area parentArea=super.show("select * from area where 1=1 and aid=?",area.getPid());
			if(null==parentArea.getPid()){
				return 3;
			}else
				return 4;
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	public void buildAreaTree(){
		try {
			list.clear();
			secondList.clear();
			thirdList.clear();
			List<Area> all=super.query("select * from area");
			for(Area a:all){//设置一级区域
				if(null==a.getPid()){
					AreaTree at=new AreaTree();
					at.setId(a.getAid());
					at.setText(a.getName());
					list.add(at);
				}
			}
			
			for(AreaTree at:list){//设置二级区域
				List<AreaTree> children=new ArrayList<AreaTree>();
				for(Area a:all){
					if(at.getId().equals(a.getPid())){
						AreaTree ati=new AreaTree();
						ati.setId(a.getAid());
						ati.setText(a.getName());
						children.add(ati);
						secondList.add(ati);//这里面保存的全是二级区域
					}
				}
				at.setChildren(children);
			}
			
			for(AreaTree at:secondList){//设置三级区域
				List<AreaTree> children=new ArrayList<AreaTree>();
				for(Area a:all){
					if(at.getId().equals(a.getPid())){
						AreaTree ati=new AreaTree();
						ati.setId(a.getAid());
						ati.setText(a.getName());
						children.add(ati);
						thirdList.add(ati);
					}
				}
				at.setChildren(children);				
			}
			
			for(AreaTree at:thirdList){//设置四级区域
				List<AreaTree> children=new ArrayList<AreaTree>();
				for(Area a:all){
					if(at.getId().equals(a.getPid())){
						AreaTree ati=new AreaTree();
						ati.setId(a.getAid());
						ati.setText(a.getName());
						children.add(ati);
					}
				}
				at.setChildren(children);				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public long counts(String pid){
		StringBuilder sql=new StringBuilder("select count(1) from area where 1=1 ");
		List<Object> params=new ArrayList<Object>();
		if(StringUtils.isNotBlank(pid)){
			sql.append(" and pid=?");
			params.add(pid);
		}else{
			sql.append(" and pid is null");
		}
		try {
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<Area> query(String pid,long page,long rows){
		StringBuilder sql=new StringBuilder("select * from area where 1=1 ");
		List<Object> params=new ArrayList<Object>();
		if(StringUtils.isNotBlank(pid)){
			sql.append(" and pid=?");
			params.add(pid);
		}else{
			sql.append(" and pid is null");
		}
		sql.append(" limit ?,?");
		params.add((page-1)*rows);
		params.add(rows);
		try {
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<Area>();
		}
	}
	
	public boolean validateName(String name){
		try {
			return super.counts("select count(1) from area where 1=1 and name=?",name)!=1;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean create(Area model){
		SqlBuilder sql=SqlBuilder.createInsert("area");
		sql.addField("aid",UUID.randomUUID().toString().replace("-","").toUpperCase());
		sql.addField("name",model.getName());
		if(StringUtils.isNotBlank(model.getPid())){
			sql.addField("pid",model.getPid());
		}
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(Area model){
		SqlBuilder sql=SqlBuilder.createUpdate("area");
		sql.addField("name",model.getName());
		sql.addWhere("aid",model.getAid());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean delete(String aid){
		try {
			return super.updates("delete from area where 1=1 and aid=?",aid)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public Area findById(String aid){
		try {
			return super.show("select * from area where 1=1 and aid=?",aid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new Area();
		}
	}
	
	public List<AreaTree> getList() {
		return list;
	}

	public List<AreaTree> getSecondList() {
		return secondList;
	}

	public List<AreaTree> getThirdList() {
		return thirdList;
	}

	public class AreaTree{
		String id;
		String text;
		List<AreaTree> children;
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getText() {
			return text;
		}
		public void setText(String text) {
			this.text = text;
		}
		public List<AreaTree> getChildren() {
			return children;
		}
		public void setChildren(List<AreaTree> children) {
			this.children = children;
		}
		
	}	
}
